package com.huiminpay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.exception.BusinessCast;
import com.huiminpay.common.cache.exception.BusinessException;
import com.huiminpay.common.cache.util.PhoneUtil;
import com.huiminpay.merchant.convert.StaffConvert;
import com.huiminpay.merchant.entity.Staff;
import com.huiminpay.merchant.entity.StoreStaff;
import com.huiminpay.merchant.mapper.StaffMapper;
import com.huiminpay.merchant.mapper.StoreStaffMapper;
import com.yh.merchant.api.StaffServiceApi;
import com.yh.merchant.dto.StaffDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author: HWW
 * @Date: 2021/8/2615:36
 * @Desc:
 */
@Service
public class StaffService implements StaffServiceApi {
    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private StoreStaffMapper storeStaffMapper;

    /**
     * 创建员工
     * @param staffDTO
     * @return
     * @throws BusinessException
     */
    @Override
    public StaffDTO createStaff(StaffDTO staffDTO) throws BusinessException {

        //校验必要参数
        if(staffDTO == null){
            BusinessCast.cast(CommonErrorCode.E_110006);
        }
        if(StringUtils.isEmpty(staffDTO.getMobile())){
            BusinessCast.cast(CommonErrorCode.E_100112);//手机号为空
        }
        if(!PhoneUtil.isMatches(staffDTO.getMobile())){
            BusinessCast.cast(CommonErrorCode.E_100109);//手机号非法
        }
        if(isExistStaffByMobile(staffDTO.getMobile(), staffDTO.getMerchantId())){
            BusinessCast.cast(CommonErrorCode.E_88888);//判断手机号不能重复，判断唯一
        }
        if(isExistStaffByUserName(staffDTO.getUsername(), staffDTO.getMerchantId())){
            throw new BusinessException(CommonErrorCode.E_200205);//判断账号唯一
        }

        Staff staff = StaffConvert.INSTANCE.dto2entity(staffDTO);
        staffMapper.insert(staff);
        return StaffConvert.INSTANCE.entity2dto(staff);
    }


    //根据手机号判断员工是否已在指定商户存在
    private boolean isExistStaffByMobile(String mobile,Long merchantId){
        Integer count = staffMapper.selectCount(new LambdaQueryWrapper<Staff>()
                .eq(Staff::getMobile, mobile)
                .eq(Staff::getMerchantId, merchantId));
        return count>0;
    }

    //根据账号判断员工是否已在指定商户存在
    private boolean isExistStaffByUserName(String userName, Long merchantId) {
        int i = staffMapper.selectCount(new LambdaQueryWrapper<Staff>()
                .eq(Staff::getUsername, userName)
                .eq(Staff::getMerchantId,merchantId));
        return i > 0;
    }


    /**
     * 给门店设置管理员-绑定用户到指定商户
     * @param storeId 门店ID
     * @param staffId 员工ID
     * @throws BusinessException
     */
    @Override
    public void bindStaffToStore(Long storeId, Long staffId) throws BusinessException {

        if(storeId==null || staffId == null){
            BusinessCast.cast(CommonErrorCode.E_110006);
        }

        StoreStaff storeStaff = new StoreStaff();
        storeStaff.setStoreId(storeId);
        storeStaff.setStaffId(staffId);
        storeStaffMapper.insert(storeStaff);
    }


}
